package com.gp.study.tomcat.bean;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import com.gp.study.tomcat.servlet.GpServlet;

public class GpServletRegistry {
	
	private static Map<String,GpServlet> servletMaps = Collections.synchronizedMap(new HashMap());
	private static String WEB_PROPERTIES = "config";
	
	static{
		doScanServlet(WEB_PROPERTIES);
	}

	private static void doScanServlet(String path) {
		// 假设config.properties 就是专门配置servlet的 没有其他内容
		ResourceBundle servletBundle = ResourceBundle.getBundle(path);
		Set<String> keySet = servletBundle.keySet();
		try{
			for(String url:keySet){
				servletMaps.put(url,(GpServlet) Class.forName(servletBundle.getString(url)).newInstance());
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

	public static boolean containsKey(String url) {
		return servletMaps.containsKey(url);
	}

	public static GpServlet getServlet(String key) {
		return servletMaps.get(key);
	}

}
